package com.complex.spring.advance.service.event;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

/**
 * 事件发布者类
 * 注意publisher 实现的是ApplicationEventPublisherAware
 * 而不是ApplicationEventPublisher
 * @author gao
 *
 */
public class MethodExecutionEventPublisher implements ApplicationEventPublisherAware{
	
	//持有applicationContext的发布器
	private ApplicationEventPublisher eventPublisher;
	public void methodToMonitor(){
		//发布开始事件
		MethodExecutionEvent beginEvt = new MethodExecutionEvent(this,"methodToMonitor",MethodExecutionStatus.BEGIN);
		this.eventPublisher.publishEvent(beginEvt);//发布事件

		//TODO 处理自己的业务
		
		//发布结束事件
		MethodExecutionEvent endEvt = new MethodExecutionEvent(this,"methodToMonitor",MethodExecutionStatus.END);
		this.eventPublisher.publishEvent(endEvt);
	}
	
	@Override
	public void setApplicationEventPublisher(
			ApplicationEventPublisher applicationEventPublisher) {
		this.eventPublisher=applicationEventPublisher;
	}
}
